/**
 * @file Define the tracking service class
 *
 * @author HengShan Lu
 */

const async = require('async');
const request = require('request');
const queryString = require('querystring');

const BaseService = require('./base-service').BaseService;
const { Configuration } = require('../constants');

/**
 * Wrap all methods about tracking
 *
 * @class TrackingService
 * @extends {BaseService}
 */
class AdminService extends BaseService {

    // 登录验证用户
    verifyUser(mobile, password, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;

        const url = `${RetailServer}/wxserver/user/login`;
        const param = {
            data: {
                mobile,
                password
            }
        };
        var postData = {
            url: url,
            method: 'POST',
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify(param)
        };
        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }
            if (error) {
                return parentCallback({
                    error: '服务端异常，请稍后再试!'
                });
            } else if (result.status) {
                parentCallback(result);
            } else {
                return parentCallback({
                    error: result.message
                });
            }
        });

    }
    
    // 钉店信息查询
    dingstoreSearch(params, parentCallback){
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;

        let url = `${RetailServer}/wxserver/store/getStoreById`;
        const param = {
            data: {
                storeNo: params.storeNo
            }
        }
        var postData = {
            url,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(param)
        }
        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {

            }

            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            }
            if (!result.status) {
                return parentCallback({ error: result.message });
            }
            parentCallback(result);
        })


    }

    // 活动列表查询
    activeSearch(params, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        let param, url;

        const { filter } = params;

        if(filter == "listdata"){
            url = `${RetailServer}/wxserver/activity/getActivity`;
            switch (params.type) {
                case '4':
                    // 查询店员
                    param = {
                        data: {
                            storeNo: params.storeNo,
                            type: params.type
                        }
                    };
                    break;
            }
        }else if(filter == "add"){
            url = `${RetailServer}/wxserver/activity/addActivity`;
            param = {
                data: params.data
            };
        }else if(filter == "save"){
            url = `${RetailServer}/wxserver/activity/updateActivity`;
            param = {
                data: params.data
            };
        }else if(filter == "edit"){
            url = `${RetailServer}/wxserver/activity/getActivityById`;
            param = {
                data: {
                    id: params.id
                }
            };
        }else if(filter == "delete"){
            url = `${RetailServer}/wxserver/activity/getActivityById`;
            param = {
                data: {
                    id: params.id
                }
            };
        }

        var postData = {
            url,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(param)
        }

        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }

            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            }
            if (!result.status) {
                return parentCallback({ error: result.message });
            }
            parentCallback(result);
        })
    }

    // 活动列表查询
    goodsSearch(data, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        let param, url;

        url = `${RetailServer}/wxserver/activity/searchGoods`;

        var postData = {
            url,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        }

        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }

            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            }
            if (!result.status) {
                return parentCallback({ error: result.message });
            }
            parentCallback(result);
        })
    }

    getGoodlistById(data, parentCallback) {
        var me = this;
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        let param, url;

        url = `${RetailServer}/wxserver/activity/searchGoods`;

        var postData = {
            url,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        }

        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                me.logger.info('格式数据出错', error);
            }

            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            }
            if (!result.status) {
                return parentCallback({ error: result.message });
            }
            parentCallback(result);
        })
    }

    doAjax(datas, parentCallback) {
        const service_servers = this.getApplicationConfiguration(Configuration.SERVICE_SERVERS);
        const RetailServer = service_servers.RetailServer;
        const { data, url } = datas;

        const postData = {
            url,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        }

        request(postData, (error, response, result) => {
            try {
                result = JSON.parse(result);
            } catch (error) {
                return parentCallback({
                    error: '数据格式有误'
                });
            }
            if (error) {
                return parentCallback({
                    error: '查询出错'
                });
            }
            if (!result.status) {
                return parentCallback({ error: result.message });
            }
            parentCallback(result);
        })
    }



}

exports.AdminService = AdminService;